﻿using System;

namespace PowerAwareBluetooth.Model
{
    /// <summary>
    /// an adapter for sending and listening to a special event
    /// </summary>
    public static class WinMessageAdapter
    {
        public enum NotificationEnum
        {
            LIST_CHANGED,
            CLOSE_MANAGER,
            RESET_LEARNING,
        } ;

        /// <summary>
        /// the windows-name of the reset-learning event
        /// </summary>
        private static string EVENT_RESET_LEARNING = "PABT_EVENT_RESET_LEARNING";

        /// <summary>
        /// the windows-name of the list-change event
        /// </summary>
        private const string EVENT_LIST_CHANGED = "PABT_EVENT_LIST_CHANGED";

        /// <summary>
        /// the windows-name of the close-manager event
        /// </summary>
        private const string EVENT_CLOSE_MANAGER = "PABT_EVENT_CLOSE_MANAGER";

        /// <summary>
        /// the named-event object that will be used to notify and listen to a list-change event
        /// </summary>
        private static NamedEvents.NamedEvents m_EventListChanged;

        /// <summary>
        /// the named-event object that will be used to notify and listen to a close-manager event
        /// </summary>
        private static NamedEvents.NamedEvents m_EventCloseManager;

                /// <summary>
        /// the named-event object that will be used to notify and listen to a reset-learning event
        /// </summary>
        private static NamedEvents.NamedEvents m_EventResetLearning;

        private static NamedEvents.NamedEvents[] m_All_Events;



        /// <summary>
        /// notifies the listeners that a change was made to the rules-list
        /// </summary>
        public static void NotifyListChanged()
        {
            DoNotify(EVENT_LIST_CHANGED);
        }

        /// <summary>
        /// notifies the listeners that a reset-learning event has occurred
        /// </summary>
        public static void NotifyResetLearning()
        {
            DoNotify(EVENT_RESET_LEARNING);
        }

        /// <summary>
        /// notifies the manager that it needs to close itself
        /// </summary>
        public static void NotifyCloseManager()
        {
            DoNotify(EVENT_CLOSE_MANAGER);
        }

        private static void DoNotify(string eventName)
        {
            NamedEvents.NamedEvents namedEvents = new NamedEvents.NamedEvents();
            namedEvents.OpenEvent(eventName);
            if (namedEvents.IsOpened)
            {
                namedEvents.PulseEvent();
            }
        }

        /// <summary>
        /// waits for the UI to notify that a change was made to the user-defined
        /// rules-list
        /// <returns>the type of event that occurred</returns>
        /// </summary>
        public static NotificationEnum WaitForMessage()
        {
            NamedEvents.NamedEvents namedEvent = NamedEvents.NamedEvents.WaitForOneOfEvents(m_All_Events);
            if (namedEvent == m_EventListChanged)
            {
                return NotificationEnum.LIST_CHANGED;
            }
            if (namedEvent == m_EventCloseManager)
            {
                return NotificationEnum.CLOSE_MANAGER;
            }
            if (namedEvent == m_EventResetLearning)
            {
                return NotificationEnum.RESET_LEARNING;
            }
            throw new NotSupportedException("Unknown event type");
        }

        /// <summary>
        /// initializes the adapter
        /// </summary>
        public static void Init()
        {
            m_EventListChanged = new NamedEvents.NamedEvents();
            m_EventCloseManager = new NamedEvents.NamedEvents();
            m_EventResetLearning = new NamedEvents.NamedEvents();

            m_EventListChanged.InitNamedEvent(EVENT_LIST_CHANGED);
            m_EventCloseManager.InitNamedEvent(EVENT_CLOSE_MANAGER);
            m_EventResetLearning.InitNamedEvent(EVENT_RESET_LEARNING);

            m_All_Events = new[] {m_EventListChanged, m_EventCloseManager, m_EventResetLearning};
        }


    }
}
